/*
* ============================================================================
*  Name     : Logger.cpp
*  Part of  : GV60
*  Copyright: Wipo Software
* ============================================================================
*/

#ifdef _DEBUG

#include <e32base.h>
#include <flogger.h>
#include "Logger.h"

_LIT(KLogFilename,  "wipo.txt");
_LIT(KLogDirectory, "WIPO");

#define LOG_CHUNK   120

void LogInit()
{
  RFileLogger iLogger;
  iLogger.Connect();
  iLogger.CreateLog(KLogDirectory, KLogFilename, EFileLoggingModeOverwrite);
  iLogger.CloseLog();
  iLogger.Close();
}

// This is the most important function.
// All other functions call this one at the end!
void LogEvent(char* n_pFilename, int n_iFileline, const TDesC8& iString1, const TDesC8& iString2, const TDesC8& iString3)
{
  TPtrC8 iFileName((TText8*)n_pFilename);
  
  HBufC8* pTmp = HBufC8::New(iString1.Length() + iString2.Length() + iString3.Length() + iFileName.Length() + 15);
  if (pTmp)
  {
    pTmp->Des().Copy(TPtrC8((TText8*)n_pFilename));
    pTmp->Des().Append(_L(":"));
    pTmp->Des().AppendNum(n_iFileline);
    pTmp->Des().Append(_L(": "));
    pTmp->Des().Append(iString1);
    if (iString2.Length() > 0)
    {
      pTmp->Des().Append(_L(" "));
      pTmp->Des().Append(iString2);
    }
    if (iString3.Length() > 0)
    {
      pTmp->Des().Append(_L(" "));
      pTmp->Des().Append(iString3);
    }

    while (pTmp->Length() > 0)
    {
      TInt iRight = (pTmp->Length() > LOG_CHUNK) ? (pTmp->Length() - LOG_CHUNK) : 0;
      RFileLogger::Write(KLogDirectory, KLogFilename, EFileLoggingModeAppend, pTmp->Left(LOG_CHUNK));
      pTmp->Des().Copy(pTmp->Right(iRight));
    }
    
    delete pTmp;
  }
}

void LogEvent(char* n_pFilename, int n_iFileline, const char* pString, TInt iNumber)
{
  LogEvent(n_pFilename, n_iFileline, TPtrC8((TText8*)pString), iNumber);
}

void LogEvent(char* n_pFilename, int n_iFileline, const char* pString, TInt iNumber1, TInt iNumber2)
{
  LogEvent(n_pFilename, n_iFileline, TPtrC8((TText8*)pString), iNumber1, iNumber2);
}

void LogEvent(char* n_pFilename, int n_iFileline, const char* pString1, const TDesC8& iString2, const TDesC8& iString3)
{
  LogEvent(n_pFilename, n_iFileline, TPtrC8((TText8*)pString1), iString2, iString3);
}

void LogEvent(char* n_pFilename, int n_iFileline, const TDesC8& iString, TInt iNumber)
{
  TBuf8<10> iNumberDes;
  iNumberDes.Num(iNumber);
  LogEvent(n_pFilename, n_iFileline, iString, iNumberDes);
}

void LogEvent(char* n_pFilename, int n_iFileline, const TDesC8& iString, TInt iNumber1, TInt iNumber2)
{
  TBuf8<20> iNumberDes;
  
  iNumberDes.Num(iNumber1);
  iNumberDes.Append(_L8(", "));
  iNumberDes.AppendNum(iNumber2);
  
  LogEvent(n_pFilename, n_iFileline, iString, iNumberDes);
}

void LogEvent(char* n_pFilename, int n_iFileline, const char* pString1, const TDesC& iString2, const TDesC& iString3)
{
  HBufC8* pTmp2 = HBufC8::New(iString2.Length() + 1);
  if (pTmp2 == NULL)
  {
    return;
  }  

  HBufC8* pTmp3 = HBufC8::New(iString3.Length() + 1);
  if (pTmp3 == NULL)
  {
    delete pTmp2;
    return;
  }  
  
  pTmp2->Des().Copy(iString2);
  pTmp3->Des().Copy(iString3);

  LogEvent(n_pFilename, n_iFileline, pString1, *pTmp2, *pTmp3);
  
  delete pTmp2;
  delete pTmp3;
}


#endif // _DEBUG

// End of file
